package cn.rengy.web.framework.configure;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;


@Configuration
@EnableScheduling
public class DefaultConfiguration{
	
	//https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter#spring-boot-starter
	
	
	@Bean
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		template.setKeySerializer(RedisSerializer.string());
		template.setValueSerializer(RedisSerializer.json());
		template.setHashKeySerializer(RedisSerializer.string());
		template.setHashValueSerializer(RedisSerializer.json());
		return template;
	}

//	@Bean
//	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
//		StringRedisTemplate template = new StringRedisTemplate();
//		template.setConnectionFactory(redisConnectionFactory);
//		template.setKeySerializer(stringRedisSerializer);
//		template.setValueSerializer(stringRedisSerializer);
//		return template;
//	}
	//改从配置文件配置
	/*@Bean
	MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
	    factory.setLocation("/tmp");
	    return factory.createMultipartConfig();
	}*/
	
	/**
	 * 如何自定义ObjectMapper
	 * https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-customize-the-jackson-objectmapper
	 * 数字全部转string可以使用spring.jackson.generator.write-numbers-as-strings=true
	 * bigdecimal只是toPlain不转string可以使用spring.jackson.generator.write-bigdecimal-as-plain=true
	 * @return
	 */
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
	    Jackson2ObjectMapperBuilderCustomizer customizer = new Jackson2ObjectMapperBuilderCustomizer() {
	        @Override
	        public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
	        	//
	            jacksonObjectMapperBuilder
	            	.serializerByType(Long.class, ToStringSerializer.instance)
	            	.serializerByType(BigDecimal.class, NumberSerializer.bigDecimalAsStringSerializer())
	            	.serializerByType(Long.TYPE, ToStringSerializer.instance)//基本类型,不设置 基本long类型不能转string
	            	
	            	.serializerByType(BigInteger.class, ToStringSerializer.instance)//mysql 无符号映射为BigInteger，有符号映射为Long
	            	;
	        }
	    };
	    return customizer;
	}

	
	@Bean
	public ScheduledExecutorService scheduledExecutorService(){
		return Executors.newScheduledThreadPool(10);
	}
	
	/**
	 * 唯一工作线程
	 * @return
	 */
	@Bean
	public ExecutorService executorService(){
		return  Executors.newSingleThreadExecutor();  
	}
	
//	@Bean
//    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
//        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
//        dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
//        return registration;
//    }

//	@ConditionalOnProperty(name = "server.ssl-mode", havingValue = "on", matchIfMissing = false)
//	protected class SslConfig{
//		@Value("${server.http.port}")
//		private Integer httpPort;
//		//ssl配置https://github.com/spring-projects/spring-boot/blob/v1.4.3.RELEASE/spring-boot-samples/spring-boot-sample-tomcat-multi-connectors/src/main/java/sample/tomcat/multiconnector/SampleTomcatTwoConnectorsApplication.java
//		@Bean
//		public EmbeddedServletContainerFactory servletContainer() {
//			TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
//			Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//			connector.setPort(httpPort);
//			tomcat.addAdditionalTomcatConnectors(connector);
//			return tomcat;
//		}
//	}
	
}
